﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using IronOne.SalesOne.BusinessObjects;
using Facade;
using System.Collections.Generic;


namespace IronOne.SalesOne.SalesOneWeb.UserControls
{
    public partial class UCSearch : System.Web.UI.UserControl
    {
        //private String action = "Manage Branch";
        //private String action="Manage Unit";
        private String action;
        String drpDownVal;
        List<SalesOneUnit> grp;
        int branchIndex;
        private int grpSelectedId = -1;
        private int brnchSelectedId = -1;
        private int unitSelectedId = -1;


        #region Public Properties

        public int GrpSelectedId 
        {
            get { return grpSelectedId; }
            set { grpSelectedId = value; }
        }
        public int BrnchSelectedId 
        {
            get { return brnchSelectedId; }
            set { brnchSelectedId = value; }
        }
        public int UnitSelectedId
        {
            get { return unitSelectedId; }
            set { unitSelectedId = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["usrType"] != null)
            {
                action = Session["usrType"].ToString();
            }
            
            if (action == "Manage Group Users")
            {
                lblHeader.Text = "Manage Group Users";
                lblBranch.Visible = false;
                lblUnit.Visible = false;
                drpdownBranch.Visible = false;
                drpdownUnit.Visible = false;
                //Label4.Visible = false;
                //Label5.Visible = false;

                if (!IsPostBack)
                {
                    FillDropDown("Group");
                    //_SETGRIDSOURCE.Value = "1";
                }

            }

            else if (action == "Manage Branch")
            {
                
                lblHeader.Text = "Manage Branch";
                lblBranch.Enabled = false;
                lblUnit.Enabled = false;
                lblBranch.Visible = false;
                lblUnit.Visible = false;
                //drpdownBranch.Enabled = false;
                //drpdownUnit.Enabled = false;
                drpdownBranch.Visible = false;
                drpdownUnit.Visible = false;
                vldBranch.Enabled = false;
                vldUnit.Enabled = false;
                //Label4.Visible = false;
                //Label5.Visible = false;

                if (!IsPostBack)
                {
                    FillDropDown("Group");
                    //_SETGRIDSOURCE.Value = "1";
                }
            }

            else if (action == "Manage Branch Users")
            {
                lblHeader.Text = "Manage Branch Users";
                lblUnit.Visible = false;
                drpdownUnit.Visible = false;
                //Label5.Visible = false;

                if (!IsPostBack)
                {
                    FillDropDown("Group");
                }
            }
            else if (action == "Manage Unit")
            {
                lblHeader.Text = "Manage Unit";

                lblUnit.Enabled = false;
                lblUnit.Visible = false;
                //drpdownUnit.Enabled = false;

                drpdownUnit.Visible = false;
                vldUnit.Enabled = false;
                //Label5.Visible = false;

                if (!IsPostBack)
                {
                    FillDropDown("Group");
                    //_SETGRIDSOURCE.Value = "1";
                }

                                  
                
            }
            else if (action == "Manage Unit Users")
            {
                if (!IsPostBack)
                {
                    FillDropDown("Group");
                }

                lblHeader.Text = "Manage Unit Users";
            }



        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //SearchWeb sw = new SearchWeb();
            //sw.GName = drpdownGroup.SelectedItem.ToString();
            //sw.BName = drpdownBranch.SelectedItem.ToString();
            //sw.UName = drpdownUnit.SelectedItem.ToString();

            if (lblHeader.Text == "Manage Branch")
            {
               // string gp = drpdownGroup.SelectedItem.ToString();
                StructureFacade sf = new StructureFacade();
                UCUserGrid ug = new UCUserGrid();
                ug.SetDataSource(grp);

                ManageBranches mb = new ManageBranches();
              //  mb.DataSource123(gp);
                
                //br=sf.GetBranchesFromGroup(gp);
                
             
            }

            else if (lblHeader.Text == "Manage group Users")
            {
            }
            else if (lblHeader.Text == "Manage Branch Users")
            {
            }
            else if (lblHeader.Text == "Manage Unit")
            {
                
            }


        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
 
            if (action == "Manage Branch")
            {
                vldBranch.Enabled = false;
                vldUnit.Enabled = false;
                vldGroup.Enabled = false;
                string pageUrl = Request.ServerVariables["URL"];
                if (pageUrl == "/Structure/ManageBranches.aspx")
                {
                    Session["BranchAc"] = "Create";
                    Response.Redirect("/Structure/AddBranch.aspx");

                    
                }
            }
            else if (action == "Manage Unit")
            {
               // vldBranch.Enabled = false;
               // vldUnit.Enabled = false;
               // vldGroup.Enabled = false;
               //vldBranch.Visible = false;

                string pageUrl = Request.ServerVariables["URL"];
                if (pageUrl == "/Structure/ManageUnits.aspx")
                {
                    Session["UnitAc"] = "Create";
                    Response.Redirect("/Structure/AddUnit.aspx");


                }
                
            }

        }
        public void FillDropDown(string type)
        {
            StructureFacade fac = new StructureFacade();

            switch (type)
            {
                case "Group":
                    List<SalesOneGroup> grp = fac.GetGroups("GroupId ASC");
                    SalesOneGroup grsel = new SalesOneGroup();
                    grsel.GroupId = -1;
                    grsel.Name = "Select...";
                    grp.Insert(0, grsel);
                    drpdownGroup.DataSource = grp;
                    drpdownGroup.DataTextField = "Name";
                    drpdownGroup.DataValueField = "GroupId";
                    drpdownGroup.DataBind();
                    
                    break;

                case "Branch":
                    if (drpdownGroup.SelectedValue != "-1")
                    {
                        List<Branch> br = fac.GetBranchesForGroup(Convert.ToInt32(drpdownGroup.SelectedValue));
                        drpdownBranch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select...";
                        br.Insert(0, brSel);
                        drpdownBranch.DataTextField = "BranchName";
                        drpdownBranch.DataValueField = "BranchId";
                        drpdownBranch.DataBind();
                    }
                    break;

                case "Unit":
                    if (drpdownBranch.SelectedValue != "-1")
                    {
                        List<SalesOneUnit> un = fac.GetUnitsInBranch(Convert.ToInt32(drpdownBranch.SelectedValue));
                        SalesOneUnit unSel = new SalesOneUnit();
                        unSel.UnitId = -1;
                        unSel.Name = "Slect";
                        un.Insert(0, unSel);
                        drpdownUnit.DataSource = un;
                        drpdownUnit.DataTextField = "Name";
                        drpdownUnit.DataValueField = "UnitId";
                        drpdownUnit.DataBind();
                    }
                    break;

                default:
                    break;
            }
        }

        protected void drpdownGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GrpSelectedId = (Convert.ToInt32(drpdownGroup.SelectedValue));

            if (lblHeader.Text == "Manage Unit")
            {
                FillDropDown("Branch");
            }
            else if (lblHeader.Text == "Manage Branch")
            {
                                  
            }
            else if (lblHeader.Text == "Manage Branch Users")
            {
                FillDropDown("Branch");
                _SETGRIDSOURCE.Value = "1";
            }
            else if (lblHeader.Text == "Manage Unit Users")
            {
                FillDropDown("Branch");
            }

        }

        protected void drpdownBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lblHeader.Text == "Manage Unit")
            {
               
            }
            else if (lblHeader.Text == "Manage Unit Users")
            {
                FillDropDown("Unit");
                _SETGRIDSOURCE.Value = "1";
            }
            
        }

        protected void drpdownUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            _SETGRIDSOURCE.Value = "1";
        }

    }
}
